﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace CLUBsInterpreter.AST
{
	/**
	 * <summary>
	 * Describes the root-node of a CLUBs game or a library.
	 * </summary>
	 **/
	class CLUBsNode : AbstractSyntaxTree
	{
		// Tree Structure
		/**
		 * <summary>
		 * The libraries referenced in the root node.
		 * </summary>
		 **/
		public List<LibraryNode> Libraries = new List<LibraryNode>();
		/**
		 * <summary>
		 * The definitions contained in the root node.
		 * </summary>
		 **/
		public List<DefinitionNode> Definitions = new List<DefinitionNode>();
		/**
		 * <summary>
		 * The 'start' action of the root node.
		 * If the node is a library, this will be null.
		 * </summary>
		 **/
		public ActionDefNode StartAction;

		// Tree Decoration
		/**
		 * <summary>
		 * Describes whether this root node is a library.
		 * </summary>
		 **/
		public bool IsLibrary = false;

		/**
		 * <see cref="AbstractSyntaxTree.Visit"/>
		 **/
		public override void Visit(Visitor v)
		{
			// Call the appropriate Visitor method
			if (m_visitedOnce)
				v.VisitCLUBsNodeSecond(this);
			else
			{
				m_visitedOnce = true;
				v.VisitCLUBsNode(this);
			}
		}

		public override void PrintTree(TextWriter outStream, int depth)
		{
			// Print CLUBsNode
			outStream.WriteLine(AbstractSyntaxTree.TabDepth(depth) + "CLUBsNode ('game')");
			// Call PrintTree for all libraries
			foreach (LibraryNode node in Libraries)
			{
				node.PrintTree(outStream, depth + 1);
			}
			// Call PrintTree for all definitions
			foreach (DefinitionNode node in Definitions)
			{
				node.PrintTree(outStream, depth + 1);
			}
			// Call PrintTree for the start action if this is not a library
			if (!IsLibrary) StartAction.PrintTree(outStream, depth + 1);
		}
	}
}
